﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_project_UpdateProject : System.Web.UI.Page
{
    static Project currentProject;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlProjectName.Items.Clear();
            ltrDetails.Text = "hide";

            var projects = ApplicationGlobalVariables.KillDaBugsDatabase.Projects;

            foreach (Project item in projects)
            {
                var projectItem = new ListItem(item.Name, item.ProjectId.ToString());
                ddlProjectName.Items.Add(projectItem);
            }

            ddlProjectName.SelectedIndex = -1;
        }
    }

    protected void ddlProjectName_SelectedIndexChanged(object sender, EventArgs e)
    {
        ltrDetails.Text = "";
        currentProject = ApplicationGlobalVariables.KillDaBugsDatabase.Projects.Single(i => i.ProjectId == int.Parse(ddlProjectName.SelectedValue));

        // bind data to control for view before editing
        txtProjectName.Text = currentProject.Name;
        txtDescription.Text = currentProject.Description;
        txtCustomMessage.Text = currentProject.CustomMessage;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        ProjectManagement projectManagement = new ProjectManagement(Master.Master.CurrentUserManagement.CurrentUser, currentProject);

        bool updateOK = projectManagement.UpdateProjectInfo(txtProjectName.Text, txtDescription.Text, txtCustomMessage.Text);
        if (!updateOK)
            ltrError.Text = "Cannot update project due to Database Error!";
        else
        {
            ltrError.Text = "Project updated successfully";
            ltrDetails.Text = "hide";

            // reset comtrols value
            txtCustomMessage.Text = "";
            txtDescription.Text = "";
            txtProjectName.Text = "";

            // reset dropdownlist
            ddlProjectName.Items.Clear();

            // update new value
            var projects = ApplicationGlobalVariables.KillDaBugsDatabase.Projects;

            foreach (Project item in projects)
            {
                var projectItem = new ListItem(item.Name, item.ProjectId.ToString());
                ddlProjectName.Items.Add(projectItem);
            }

            ddlProjectName.SelectedIndex = -1;
        }
    }
}